import axios from 'axios';
import { RootState } from '.';

// 轮播图
const CAROUSEL_URL = '/carousel';

// 猜你喜欢
const GUESS_URL = '/guess';

// 首页列表
const CHANNEL_URL = '/channel';


const initialState = {
  carousels: [],
  activeCarouselIndex: 0,
  gradientVisible: true,
  guess: [],
  channels: [],
  pagination: {
    current: 1,
    total: 0,
    hasMore: true,
  }
};

const homeModel = {
  namespace: 'home',
  state: initialState,
  reducers: {
    setState(state = initialState, {payload}) {
      return {
        ...state,
        ...payload,
      };
    },
  },
  effects: {
    *fetchCarousels(_, {call, put}) {
      const {data} = yield call(axios.get, CAROUSEL_URL);
      console.log('轮播图数据', data);
      yield put({
        type: 'setState',
        payload: {
          carousels: data,
        },
      });
    },
    *fetchGuess(_, {call, put}) {
      const {data} = yield call(axios.get, GUESS_URL);
      yield put({
        type: 'setState',
        payload: {
          guess: data,
        },
      });
    },
    *fetchChannels({callback, payload}, {call, put, select}) {
      const {channels, pagination} = yield select((state) => state.home);
      let page = 1;
      if(payload && payload.loadMore) {
        page = pagination.current + 1;
      }
      const {data} = yield call(axios.get, CHANNEL_URL, {
        params: {
          page,
        }
      });
      let newChannels = data.results;
      if(payload && payload.loadMore) {
        newChannels = channels.concat(newChannels);
      }
      
      yield put({
        type:'setState',
        payload: {
          channels: newChannels,
          pagination: {
            current: data.pagination.current,
            total: data.pagination.total,
            hasMore: newChannels.length < data.pagination.total,
          },
        }
      })
      if(typeof callback === 'function') {
        callback();
      }
    }
  },
};

export default homeModel;
